Skip to content

perf(network): Replace extern global network variables with const ones#2726

Merged
xezon merged 4 commits into
TheSuperHackers:mainfrom
Caball009:refactor_network_globals
May 20, 2026
Merged

perf(network): Replace extern global network variables with const ones#2726
xezon merged 4 commits into
TheSuperHackers:mainfrom
Caball009:refactor_network_globals

Conversation

@Caball009
Copy link
Copy Markdown

@Caball009 Caball009 commented May 17, 2026

This PR removes the "-RunAhead" command line so that a couple of global network variables can be marked static const static constexpr const for safety and performance. Considering that the "RunAhead" command is for development only and likely rarely used, I think it's ok to get rid of it.

The current setup with extern in the header and definition in the source file causes the compiler to miss out on optimizations. It's possible that link time optimization would fix this, but that's not currently used.

TODO:

  • Replicate in Generals.

@Caball009 Caball009 added Minor Severity: Minor < Major < Critical < Blocker Performance Is a performance concern Network Anything related to network, servers labels May 17, 2026
@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 17, 2026

Greptile Summary

This PR converts five mutable extern network global variables (MIN_LOGIC_FRAMES, MAX_FRAMES_AHEAD, MIN_RUNAHEAD, FRAME_DATA_LENGTH, FRAMES_TO_KEEP) into static constexpr const inline constants in NetworkDefs.h, and removes the -RunAhead command-line option that was the only runtime mutation point for three of them.

  • NetworkDefs.h: Declarations moved from extern to static constexpr const; derived constants (FRAME_DATA_LENGTH, FRAMES_TO_KEEP) are now computed at compile time from MAX_FRAMES_AHEAD. Using static gives internal linkage, preventing ODR violations across translation units.
  • NetworkUtil.cpp: Variable definitions deleted; tweak comment relocated to the header alongside the constants.
  • CommandLine.cpp (both Generals and GeneralsMD): parseRunAhead() and the { "-RunAhead", … } table entry removed identically in both targets.

Confidence Score: 5/5

Safe to merge — the change is a straightforward constexpr promotion with no behavioural difference at runtime.

The network constants had fixed values in the source file and the only mutation path (-RunAhead) is being intentionally removed. Making them static constexpr in the header is correct and consistent: static gives internal linkage (no ODR issue across TUs), constexpr enables compile-time evaluation, and the derived constants FRAME_DATA_LENGTH and FRAMES_TO_KEEP are computed from MAX_FRAMES_AHEAD at compile time just as they were at startup before. Both Generals and GeneralsMD are updated identically.

No files require special attention.

Important Files Changed

Filename Overview
Core/GameEngine/Include/GameNetwork/NetworkDefs.h Replaces five extern variable declarations with static constexpr const inline definitions; upgrades other static const constants to constexpr. Change is correct — static linkage prevents ODR issues and constexpr enables full compile-time evaluation.
Core/GameEngine/Source/GameNetwork/NetworkUtil.cpp Removes the five global variable definitions that are now inline constexpr in the header; the tweak comment is moved to the header alongside the constants.
Generals/Code/GameEngine/Source/Common/CommandLine.cpp Removes parseRunAhead() and the -RunAhead entry from paramsForEngineInit, consistent with making the network constants compile-time-only.
GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp Identical changes to Generals/ counterpart, correctly replicating the removal of parseRunAhead() and -RunAhead for Zero Hour.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["NetworkDefs.h (header)"] -->|"Before: extern Int"| B["NetworkUtil.cpp\ndefinitions"]
    A -->|"After: static constexpr const"| C["Inline compile-time constants"]
    D["CommandLine.cpp (-RunAhead)"] -->|"Before: modifies at runtime"| B
    D -->|"After: removed"| E["No runtime mutation path"]
    C -->|"Each TU gets own copy\n(internal linkage)"| F["Compiler inlines values\nenabling optimizations"]
Loading

Reviews (4): Last reviewed commit: "Replicated in Generals." | Re-trigger Greptile

Comment thread Core/GameEngine/Include/GameNetwork/NetworkDefs.h Outdated
Copy link
Copy Markdown

@xezon xezon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok

Comment thread Core/GameEngine/Include/GameNetwork/NetworkDefs.h Outdated
@Caball009 Caball009 force-pushed the refactor_network_globals branch from 359875d to de40ca3 Compare May 19, 2026 22:15
@Caball009
Copy link
Copy Markdown
Author

Rebased to include the fix for the CI Replay checker.

@xezon xezon changed the title perf(network): Remove extern and add const to global network variables perf(network): Replace extern global network variables with const ones May 20, 2026
@xezon xezon merged commit c1bce4f into TheSuperHackers:main May 20, 2026
32 of 33 checks passed
@Caball009 Caball009 deleted the refactor_network_globals branch May 20, 2026 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Minor Severity: Minor < Major < Critical < Blocker Network Anything related to network, servers Performance Is a performance concern

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants